package com.xyc.student.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyc.student.common.Result;
import com.xyc.student.entity.Score;
import com.xyc.student.service.IScoreService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Joel
 * @date 2022/4/7
 */

@RestController
@RequestMapping("/score")
public class ScoreController {

    @Resource
    IScoreService scoreService;

    @GetMapping
    public Result findAll() {
        return Result.success(scoreService.list());
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String mycollege,
                           @RequestParam(defaultValue = "") Integer grade,
                           @RequestParam(defaultValue = "") String subject) {
        QueryWrapper<Score> queryWrapper = new QueryWrapper<>();
        if (!"".equals(mycollege)) {
            queryWrapper.like("college", mycollege);
        }
        if (grade != null) {
            queryWrapper.like("grade", grade);
        }
        if (!"".equals(subject)) {
            queryWrapper.like("subject", subject);
        }

        return Result.success(scoreService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}
